<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="学校代码">
              <a-input v-model="queryParam.xxdm" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="学校名称">
              <a-input v-model="queryParam.xxmc" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="行政归属">
                <a-select style="width:33%" @change="provinceChange" v-model="queryParam.xzqhsdm" :allowClear="true">
                  <a-select-option v-for="i in selectMenu.province" :key="i.areaCode" :value="i.areaCode">{{ i.name }}</a-select-option>
                </a-select>
                <a-select style="width:33%" @change="cityChange" v-model="queryParam.xzqhcdm" :allowClear="true">
                  <a-select-option v-for="i in selectMenu.city" :key="i.areaCode" :value="i.areaCode">{{ i.name }}</a-select-option>
                </a-select>
                <a-select style="width:33%" v-model="queryParam.xzqhxdm" :allowClear="true">
                  <a-select-option v-for="i in selectMenu.area" :key="i.areaCode" :value="i.areaCode">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection">


      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>

    </s-table>

  </a-card>
</template>

<script>
import { STable } from '~'

export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      visible: false,
      editId: null,
      // 查询参数
      queryParam: {},
      postParams: {
        sssfbz: true
      },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学校代码',
          dataIndex: 'xxdm'
        },
        {
          title: '学校名称',
          dataIndex: 'xxmc'
        },
        {
          title: '行政归属',
          dataIndex: 'xzqhsdmc'
        },
        {
          title: '学校性质',
          dataIndex: 'xxxzmc'
        },
        {
          title: '学校地址',
          dataIndex: 'xxdz'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.base.schoolInfoList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: false }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      confirmLoading: false,
      selectMenu: {}
    }
  },
  created () {
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.areaList()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          province: res[0].data
        }
      })
    },
    provinceChange (v) {
      this.$set(this.queryParam, 'xzqhcdm', '')
      this.$set(this.queryParam, 'xzqhxdm', '')
      if (v) {
        this.$api.base.areaList({parentCode: v}).then(res => {
          this.$set(this.selectMenu, 'city', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'city', [])
      }
      this.$set(this.selectMenu, 'area', [])
    },
    cityChange (v) {
      this.$set(this.queryParam, 'xzqhxdm', '')
      if (v) {
        this.$api.base.areaList({parentCode: v}).then(res => {
          this.$set(this.selectMenu, 'area', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'area', [])
      }
    },
    resetForm () {
      this.queryParam = {}
      this.selectMenu.city = []
      this.selectMenu.area = []
    }
  }
}
</script>
